package com.bcqn.service;

import com.bcqn.dao.HousingCollectionMapper;
import com.bcqn.dao.HousingInformationMapper;
import com.bcqn.model.HousingCollection;
import com.bcqn.model.HousingInformation;
import com.bcqn.model.vo.RentingQueryObj;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * @author liugang
 * @version 1.0
 * @date 2020/4/17 16:34
 */
@Service
public class RentingService {

    @Resource
    private HousingInformationMapper housingInformationMapper;

    @Resource
    private HousingCollectionMapper housingCollectionMapper;


    /**
     * 添加新的房间的信息  userId是从用户的session中去获取的
     * @param housingInformation
     * @return
     */
    public Object add(int userId, HousingInformation housingInformation) {
        housingInformation.setUserId(userId);
        return housingInformationMapper.insertSelective(housingInformation);
    }

    /**
     * 租房的详情
     * @param id 房子对应的id
     * @return
     */
    public HousingInformation detail(Long id) {
        return housingInformationMapper.selectByPrimaryKey(id);
    }

    /**
     * 分页查询用户的集合信息
     * @param rentingQueryObj
     * @param pageNO
     * @return
     */
    public PageInfo<HousingInformation> getList(RentingQueryObj rentingQueryObj, int pageNO,int pageSize) {
        PageHelper.startPage(pageNO,pageSize);
        List<HousingInformation> housingInformations = housingInformationMapper.getHousInfoByPage(rentingQueryObj);
        PageInfo<HousingInformation> info  =new PageInfo<HousingInformation>(housingInformations);
        return info;
    }

    /**
     * 根据用户的使用的id来添加对应的收藏的信息
     * @param userId
     * @param houingId
     */
    public void collect(int userId,int houingId) {
        HousingCollection housingCollection = new HousingCollection();
        housingCollection.setUserId(userId);
        housingCollection.setHouingId(houingId);
        housingCollectionMapper.insertSelective(housingCollection);
    }

    /**
     * 根据用户的使用的id和房源的对应的Id来删除对应的用户的信息
     * @param userId
     * @param houingId
     */
    public void delCollect(int userId,int houingId) {
        housingCollectionMapper.deleteByUserIdAndHousingId(userId,houingId);
    }

    public PageInfo<HousingInformation> getMyPost(Long userId, int pageSize, int currentPage){
        PageHelper.startPage(currentPage, pageSize);
        List<HousingInformation> list = housingInformationMapper.getMyPost(userId);
        return new PageInfo<>(list);
    }

    public PageInfo<HousingInformation> getMyCollection(Long userId, int pageSize, int currentPage){
        PageHelper.startPage(currentPage, pageSize);
        List<HousingInformation> list = housingInformationMapper.getMyCollection(userId);
        return new PageInfo<>(list);
    }

    /**
     * 根据id来删除发布的对应的租房的信息
     * @param id
     */
    public void delete(Integer id) {
        housingInformationMapper.deleteByPrimaryKey( new Long(id));
    }


    public PageInfo<HousingInformation> myRentingCollection(Long userId, int pageSize, int currentPage) {
        PageHelper.startPage(currentPage, pageSize);
        List<HousingInformation> list = housingInformationMapper.myRentingCollection(userId);
        PageInfo<HousingInformation> info  =new PageInfo<HousingInformation>(list);
        return info;
    }


}
